﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace IRTest
{
    class Program
    {
        static void Main(string[] args)
        {

            using (SerialPort p = new SerialPort("COM1"))
            {
                p.Open();

                byte[] signal = Encoding.ASCII.GetBytes("W");

                p.Write(signal, 0, signal.Length);

                byte[] bytes = new byte[131200];

                p.ReadBufferSize = 1024;

                int numBytesToRead = bytes.Length;
                int numBytesRead = 0;

                while (numBytesToRead > 0)
                {
                    // Read may return anything from 0 to numBytesToRead.
                    int n = p.Read(bytes, numBytesRead, numBytesToRead);

                    // The end of the file is reached.
                    if (n == 0)
                    {
                        break;
                    }
                    else
                    {
                        Console.Write(Encoding.ASCII.GetString(bytes, numBytesRead, n));
                    }

                    numBytesRead += n;
                    numBytesToRead -= n;                    
                }

                p.Close();
            }
        }
    }
}
